package com.zebraphp.devtool.util;

import android.os.Environment;
import android.os.StatFs;

/*=============================SD卡相关的辅助类====================================
    需要加入权限
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
	<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
 *================================================================================*/
public class ISDCard {

    private ISDCard() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static String getAlbumPath() {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
    }

    /*** 获取系统存储路径 */
    public static String getRootDirectoryPath() {
        return Environment.getRootDirectory().getAbsolutePath();
    }

    /**
     * 判断内部SD卡是否可用
     */
    public static boolean isInnerSDCardEnable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 获取内部SD卡路径
     */
    public static String getInnerSDCardPath() {
//        return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
        return Environment.getExternalStorageDirectory().toString();
    }

    /**
     * 获取SD卡的剩余容量 单位byte
     */
    public static long getSDCardAllSize() {
        if (isInnerSDCardEnable()) {
            StatFs stat = new StatFs(getInnerSDCardPath());
            // 获取空闲的数据块的数量
            long availableBlocks = (long) stat.getAvailableBlocks() - 4;
            // 获取单个数据块的大小（byte）
            long freeBlocks = stat.getAvailableBlocks();
            return freeBlocks * availableBlocks;
        }
        return 0;
    }

    /**
     * 获取指定路径所在空间的剩余可用容量字节数，单位byte
     * @return 容量字节 SDCard可用空间，内部存储可用空间
     */
    public static long getFreeBytes(String filePath) {
        // 如果是sd卡的下的路径，则获取sd卡可用容量
        if (filePath.startsWith(getInnerSDCardPath())) {
            filePath = getInnerSDCardPath();
        } else {
            // 如果是内部存储的路径，则获取内存存储的可用容量
            filePath = Environment.getDataDirectory().getAbsolutePath();
        }
        StatFs stat = new StatFs(filePath);
        long availableBlocks = (long) stat.getAvailableBlocks() - 4;
        return stat.getBlockSize() * availableBlocks;
    }


//    /**
//     * 获取外置SD卡路径
//     * 参考文章:http://www.jianshu.com/p/de1932ffb1cc
//     * ！！！不建议使用，不准确
//     * @return 应该就一条记录或空
//     */
//    public static String[] getExtSDCardPath(Context context) {
//        StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
//        try {
//            Class<?>[] paramClasses = {};
//            Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", paramClasses);
//            getVolumePathsMethod.setAccessible(true);
//            Object[] params = {};
//            Object invoke = getVolumePathsMethod.invoke(storageManager, params);
//            return (String[]) invoke;
//        } catch (NoSuchMethodException e1) {
//            e1.printStackTrace();
//        } catch (IllegalArgumentException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    /**
//     * 检测SD卡 是否挂载
//     * 参考文章:http://www.jianshu.com/p/de1932ffb1cc
//     *
//     * @return true or false
//     */
//    public static boolean checkSDCardIsMounted(Context context, String mountPath) {
//        if (mountPath == null) {
//            return false;
//        }
//        StorageManager storageManager = (StorageManager) context
//                .getSystemService(Context.STORAGE_SERVICE);
//        try {
//            Method getVolumeState = storageManager.getClass().getMethod("getVolumeState", String.class);
//            String state = (String) getVolumeState.invoke(storageManager, mountPath);
//            return Environment.MEDIA_MOUNTED.equals(state);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return false;
//    }

}
